package com.will.weiyuekotlin.ui.weather

import android.os.Bundle
import android.view.View
import com.will.weiyuekotlin.R
import com.will.weiyuekotlin.bean.WeatherBean
import com.will.weiyuekotlin.component.ApplicationComponent
import com.will.weiyuekotlin.component.DaggerHttpComponent
import com.will.weiyuekotlin.ui.base.BaseActivity
import com.will.weiyuekotlin.ui.news.contract.WeatherContract
import com.will.weiyuekotlin.ui.weather.presenter.WeatherPresenter
import com.will.weiyuekotlin.widget.SimpleMultiStateView
import kotlinx.android.synthetic.main.activity_weather.*
import java.util.ArrayList

/**
 * @author: ywj
 * @date: 2018/12/27 11:49
 */

class WeatherActivity : BaseActivity<WeatherPresenter>(), WeatherContract.View {


    override fun getContentLayout(): Int = R.layout.activity_weather

    override fun getSimpleMultiStateView(): SimpleMultiStateView? = null

    override fun initInjector(appComponent: ApplicationComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this)
    }

    override fun bindView(view: View, savedInstanceState: Bundle?) {
    }

    override fun initData() {
        mPresenter?.getData("河南省", "郑州市","")
    }

    override fun loadData(weatherBean: WeatherBean?) {
        weatherBean?.let {
            if (weatherBean.status != 200) {
                return
            }
            val forecast1hMap = it.data?.forecast_1h
            forecast1hMap?.values?.let {
                for (value in it) {
                    tv_current_temperature.text = value.degree
                    break
                }
            }

            val forecast24hMap = it.data?.forecast_24h
            val iterator = forecast24hMap?.iterator()
            val weatherDays = ArrayList<WeatherBean.DataBean.Forecast24hBean>()
            while (iterator?.hasNext()!!) {
                val entry = iterator.next()

                when (entry.key) {
                    "0"->{}
                    "1" -> {
                        weatherDays.add(entry.value)
                        tv_today_max_temperature.text = """${entry.value.max_degree}°C"""
                        tv_today_min_temperature.text = """${entry.value.min_degree}°C"""

                        tv_today_sun.text = entry.value.day_weather
                        tv_today_air.text = entry.value.day_wind_direction + entry.value.day_wind_power + "级"
                    }
                    else->{
                        weatherDays.add(entry.value)
                    }
                }

            }
            var content:StringBuilder= StringBuilder()
            for (weatherDay in weatherDays) {
                content
                        .append(weatherDay.time).append("  ")
                        .append(weatherDay.max_degree).append("°C/")
                        .append(weatherDay.min_degree).append("°C ")
                        .append(weatherDay.day_weather).append("  ")
                        .append(weatherDay.day_wind_direction).append("\n")
            }
            tv_content.text=content.toString()

        }
    }

}
